#!/usr/bin/env ruby

LKP_SRC = ENV['LKP_SRC'] || File.dirname(File.dirname(File.realpath($PROGRAM_NAME)))

require "#{LKP_SRC}/lib/string_ext"
require "#{LKP_SRC}/lib/array_ext"
require "#{LKP_SRC}/lib/tests/stats"

stats = LKP::Stats.new

test_item = ''
build_type = ''

# vmmalloc_memalign/TEST1: SETUP (check/nondebug)
while (line = $stdin.gets)
  line = line.resolve_invalid_bytes
  case line
  when %r{^(.+)/TEST[0-9]+: SETUP \(.+/(.+)\)$}
    test_item = Regexp.last_match[1]
    build_type = Regexp.last_match[2]
  when %r{^(.+)/(TEST[0-9]+): (PASS|FAIL|SKIP)}
    item = Regexp.last_match[1]
    name = Regexp.last_match[2]
    next unless test_item == item

    stats.add "#{item}_#{name}_#{build_type}", Regexp.last_match[3]
  when %r{RUNTESTS: stopping: (.+)/(TEST[0-9]+) failed}
    item = Regexp.last_match[1]
    name = Regexp.last_match[2]

    next unless test_item == item

    stats.add "#{item}_#{name}_#{build_type}", 'fail'
  when %r{RUNTESTS: stopping: (.+)/(TEST[0-9]+) timed out}
    item = Regexp.last_match[1]
    name = Regexp.last_match[2]
    next unless test_item == item

    stats.add "#{item}_#{name}_#{build_type}", 'timeout'
  when %r{^(.+)/(TEST[0-9]+): SKIP}
    item = Regexp.last_match[1]
    name = Regexp.last_match[2]

    stats.add "#{item}_#{name}", 'test_skip'
  end
end

stats.show
